package functionalTest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import util.DateUtil;
import domain.Envelope;
import domain.EnvelopeType;
import domain.Mission;
import domain.MissionStatus;
import domain.POS;
import domain.Route;
import domain.Schedule;
import domain.ScheduleStatus;

public class ScheduleUnitTest {
	Schedule schedule;
	@Before
	public void setUp() throws Exception {
		schedule = new Schedule();
		schedule.setRoute(Route.R11);
		schedule.setDate(DateUtil.getTimestampFromDDMMYYYY("23/03/2011", 0));
		schedule.setStatus(ScheduleStatus.unpriortized);
		
		POS pos = new POS();
			pos.setName("BTNC heuartead qr");
			pos.setLatitute(13.711428);
			pos.setLongtitute(100.594103);
			
		POS pos2 = new POS();
			pos2.setName("The Mall Ramkhamhaeng 3");
			pos2.setLatitute(13.752729);
			pos2.setLongtitute(100.609644);
			
		POS pos3 = new POS();
			pos3.setName("Seconsquare");
			pos3.setLatitute(13.695265);
			pos3.setLongtitute(100.648187);
			
		POS pos4 = new POS();
			pos4.setName("Central Department Store");
			pos4.setLatitute(13.669594);
			pos4.setLongtitute(100.634778);
			
		Envelope e1 = new Envelope();
			e1.setEnvelopeType(EnvelopeType.refill);
			e1.setEnvelopId("D001");
			e1.setNoOfProduct(10);
		Envelope e2 = new Envelope();
			e2.setEnvelopeType(EnvelopeType.refill);
			e2.setEnvelopId("D002");
			e2.setNoOfProduct(5);
		Envelope e3 = new Envelope();
			e3.setEnvelopeType(EnvelopeType.refill);
			e3.setEnvelopId("D003");
			e3.setNoOfProduct(4);
			
		Mission mission = new Mission();
			mission.setId(1);
			mission.setFromPOS(pos);
			mission.setToPOS(pos2);
			mission.setPriority(1);
			mission.setStatus(MissionStatus.complete);
			mission.addEnvelopes(e1);
			
		Mission mission2 = new Mission();
			mission2.setId(2);
			mission2.setFromPOS(pos2);
			mission2.setToPOS(pos3);
			mission2.setPriority(2);
			mission2.setStatus(MissionStatus.incomplete);
			mission2.addEnvelopes(e2);
			
		Mission mission3 = new Mission();
			mission3.setId(3);
			mission3.setFromPOS(pos3);
			mission3.setToPOS(pos4);
			mission3.setPriority(3);
			mission3.setStatus(MissionStatus.incomplete);
			mission3.addEnvelopes(e3);
		
		schedule.addMission(mission);
		schedule.addMission(mission2);
		schedule.addMission(mission3);
	}

	@After
	public void tearDown() throws Exception {
		schedule = null;
	}

	@Test
	public void testGetNoOfCompletedMissions() {
		int complete = schedule.getNoOfCompletedMissions();
		assertEquals(1, complete);
	}
	
	@Test
	public void testGetNoOfAllMissions() {
		int all = schedule.getNoOfAllMissions();
		assertEquals(3, all);
	}
	
	@Test
	public void testIsCompleted() {
		boolean complete = schedule.isCompleted();
		assertTrue(!complete);
	}
	
	@Test
	public void testCheckAndUpdateCompleteStatus(){
		schedule.checkAndUpdateCompleteStatus();
		assertEquals(ScheduleStatus.unpriortized, schedule.getStatus());
	}
	
	@Test
	public void testUpdateCompleteMission(){
		schedule.updateCompleteMission(2);
		assertEquals(schedule.getMissions().get(1).getStatus(), MissionStatus.complete);
	}
	
	@Test
	public void testSortMissionByPriority(){
		schedule.sortMisionsByPriority();
		int i=1;
		for(Mission m: schedule.getMissions()){
			assertEquals(i, m.getPriority());
			i++;
		}
		
	}
	
	@Test
	public void testGetPrioritizedPOS(){
		List<POS> posList = schedule.getPrioritizedPOS();
		assertEquals("BTNC heuartead qr", posList.get(0).getName());
		assertEquals("The Mall Ramkhamhaeng 3", posList.get(1).getName());
		assertEquals("Seconsquare", posList.get(2).getName());
		assertEquals("Central Department Store", posList.get(3).getName());
	}
	
	@Test
	public void testIsReachDuedDate1(){
		assertTrue(schedule.isReachDuedDate());
	}
	
	@Test
	public void testIsReachDuedDate2(){
		schedule.setDate(DateUtil.getCurrentTimestamp());
		assertTrue(schedule.isReachDuedDate());
	}
	
	@Test
	public void testIsReachDuedDate3(){
		Date now = new Date();  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(now);  
		cal.add(Calendar.DAY_OF_YEAR, 1); 
		Date tomorrow = cal.getTime(); 
		schedule.setDate(DateUtil.getTimestampFromDate(tomorrow));
		assertFalse(schedule.isReachDuedDate());
	}
	
	@Test
	public void testIsItPassedSchedule1(){
		assertTrue(schedule.isItPassedSchedule());
	}
	
	@Test
	public void testIsItPassedSchedule2(){
		schedule.setDate(DateUtil.getCurrentTimestamp());
		assertFalse(schedule.isItPassedSchedule());
	}
	
	@Test
	public void testIsItPassedSchedule3(){
		Date now = new Date();  
		Calendar cal = Calendar.getInstance();  
		cal.setTime(now);  
		cal.add(Calendar.DAY_OF_YEAR, 1); 
		Date tomorrow = cal.getTime(); 
		schedule.setDate(DateUtil.getTimestampFromDate(tomorrow));
		assertFalse(schedule.isItPassedSchedule());
	}
	
	@Test
	public void testUpdatePriorityAndFromPOS(){
	POS pos2 = new POS();
		pos2.setName("The Mall Ramkhamhaeng 3");
		pos2.setLatitute(13.752729);
		pos2.setLongtitute(100.609644);
		
	POS pos4 = new POS();
		pos4.setName("Central Department Store");
		pos4.setLatitute(13.669594);
		pos4.setLongtitute(100.634778);
		
		schedule.updatePriorityAndFromPOS(pos2, pos4, 3);
		
		Mission updatedMission = schedule.getMissions().get(2);
		
		assertEquals(pos2.getName(), updatedMission.getFromPOS().getName());
		assertEquals(3, updatedMission.getPriority());
	}
	
	@Test
	public void testGetUnPrioritizePOS(){
		List<POS> posList = schedule.getUnPrioritizePOS(); // Get all toPOS
		assertEquals(3, posList.size());
	}
}
